home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / FilteredImageSource.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  139 lines

  1. /*
  2.  * @(#)FilteredImageSource.java    1.18 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17. import java.awt.Image;
  18. import java.awt.image.ImageFilter;
  19. import java.awt.image.ImageConsumer;
  20. import java.awt.image.ImageProducer;
  21. import java.util.Hashtable;
  22. import java.awt.image.ColorModel;
  23.  
  24. /**
  25.  * This class is an implementation of the ImageProducer interface which
  26.  * takes an existing image and a filter object and uses them to produce
  27.  * image data for a new filtered version of the original image.
  28.  * Here is an example which filters an image by swapping the red and
  29.  * blue compents:
  30.  * <pre>
  31.  * 
  32.  *    Image src = getImage("doc:///demo/images/duke/T1.gif");
  33.  *    ImageFilter colorfilter = new RedBlueSwapFilter();
  34.  *    Image img = createImage(new FilteredImageSource(src.getSource(),
  35.  *                            colorfilter));
  36.  * 
  37.  * </pre>
  38.  *
  39.  * @see ImageProducer
  40.  *
  41.  * @version    1.18 07/01/98
  42.  * @author     Jim Graham
  43.  */
  44. public class FilteredImageSource implements ImageProducer {
  45.     ImageProducer src;
  46.     ImageFilter filter;
  47.  
  48.     /**
  49.      * Constructs an ImageProducer object from an existing ImageProducer
  50.      * and a filter object.
  51.      * @see ImageFilter
  52.      * @see java.awt.Component#createImage
  53.      */
  54.     public FilteredImageSource(ImageProducer orig, ImageFilter imgf) {
  55.     src = orig;
  56.     filter = imgf;
  57.     }
  58.  
  59.     private Hashtable proxies;
  60.  
  61.     /**
  62.      * Adds an ImageConsumer to the list of consumers interested in
  63.      * data for this image.
  64.      * @see ImageConsumer
  65.      */
  66.     public synchronized void addConsumer(ImageConsumer ic) {
  67.     if (proxies == null) {
  68.         proxies = new Hashtable();
  69.     }
  70.     if (!proxies.containsKey(ic)) {
  71.         ImageFilter imgf = filter.getFilterInstance(ic);
  72.         proxies.put(ic, imgf);
  73.         src.addConsumer(imgf);
  74.     }
  75.     }
  76.  
  77.     /**
  78.      * Determines whether an ImageConsumer is on the list of consumers 
  79.      * currently interested in data for this image.
  80.      * @return true if the ImageConsumer is on the list; false otherwise
  81.      * @see ImageConsumer
  82.      */
  83.     public synchronized boolean isConsumer(ImageConsumer ic) {
  84.     return (proxies != null && proxies.containsKey(ic));
  85.     }
  86.  
  87.     /**
  88.      * Removes an ImageConsumer from the list of consumers interested in
  89.      * data for this image.
  90.      * @see ImageConsumer
  91.      */
  92.     public synchronized void removeConsumer(ImageConsumer ic) {
  93.     if (proxies != null) {
  94.         ImageFilter imgf = (ImageFilter) proxies.get(ic);
  95.         if (imgf != null) {
  96.         src.removeConsumer(imgf);
  97.         proxies.remove(ic);
  98.         if (proxies.isEmpty()) {
  99.             proxies = null;
  100.         }
  101.         }
  102.     }
  103.     }
  104.  
  105.     /**
  106.      * Adds an ImageConsumer to the list of consumers interested in
  107.      * data for this image, and immediately starts delivery of the
  108.      * image data through the ImageConsumer interface.
  109.      * @see ImageConsumer
  110.      */
  111.     public void startProduction(ImageConsumer ic) {
  112.     if (proxies == null) {
  113.         proxies = new Hashtable();
  114.     }
  115.     ImageFilter imgf = (ImageFilter) proxies.get(ic);
  116.     if (imgf == null) {
  117.         imgf = filter.getFilterInstance(ic);
  118.         proxies.put(ic, imgf);
  119.     }
  120.     src.startProduction(imgf);
  121.     }
  122.  
  123.     /**
  124.      * Requests that a given ImageConsumer have the image data delivered
  125.      * one more time in top-down, left-right order.  The request is
  126.      * handed to the ImageFilter for further processing, since the
  127.      * ability to preserve the pixel ordering depends on the filter.
  128.      * @see ImageConsumer
  129.      */
  130.     public void requestTopDownLeftRightResend(ImageConsumer ic) {
  131.     if (proxies != null) {
  132.         ImageFilter imgf = (ImageFilter) proxies.get(ic);
  133.         if (imgf != null) {
  134.         imgf.resendTopDownLeftRight(src);
  135.         }
  136.     }
  137.     }
  138. }
  139.